package com.shenmazong.demorequestparam.controller;

import com.shenmazong.demorequestparam.pojo.AddIntegers;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@Slf4j
@Api(tags = "用户API接口")
public class IndexController {

    @GetMapping("/")
    public String index() {
        return "index";
    }

    @ApiOperation(value = "计算和", notes="计算两个数的和")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "x", value = "x值", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "y", value = "y值", required = true, paramType = "query", dataType = "int")
    })
    @ApiResponses({
            @ApiResponse(code=4001,message="请求参数没填好"),
            @ApiResponse(code=4002,message="请求路径没有或页面跳转路径不对")
    })
    @GetMapping(value = "/add")
    @ResponseBody
    public Object addByGet(@RequestParam("x") Integer x, @RequestParam("y") Integer y) {
        return (x+y);
    }

    @PostMapping(value = "/add")
    @ResponseBody
    public Object addByPost(@RequestParam("x") Integer x, @RequestParam("y") Integer y) {
        return (x+y);
    }

    @PostMapping(value = "/addByJson")
    @ResponseBody
    public Object addByJson(@RequestBody AddIntegers addIntegers) {
        return addIntegers.getX()+addIntegers.getY();
    }

    @GetMapping(value = "/addByPath/{x}/{y}")
    @ResponseBody
    public Object addByPath(
            @PathVariable(name = "x", required = true) Integer x,
            @PathVariable(name = "y", required = true) Integer y) {
        return x+y;
    }
}
